<template>
  <n-modal
    v-model:show="modelValue.toDelete"
    :mask-closable="false"
    preset="dialog"
    title="确认"
    content="你确认"
    positive-text="确认"
    negative-text="算了"
    @positive-click="onPositiveClick"
    @negative-click="modelValue.toDelete = false"
  />
</template>

<script
  lang="ts"
  setup
  generic="T extends object & { toEdit: boolean,toDelete:boolean,toDealRowId: number|undefined,type:number}"
>
import { StateMoal } from "@model/StateMoal";
import { TaskIdsModal } from "@model/stage";
import UseFieldTree from "@pinias/UseFieldTree";
import UseStage from "@pinias/UseStage";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import useTask from "@pinias/UseTask";
import pinia from "@pinias/pinia";
import { NModal, useMessage } from "naive-ui";
import { computed } from "vue";

// 全局数据
const { getSurfaceSetting } = UseSurfaceSetting(pinia);
const surfaceSetting = getSurfaceSetting();
const { getStageList } = UseStage(pinia);
const stageList = getStageList();
const { getTaskList } = useTask(pinia);
const taskList = getTaskList();
const { getFieldTree } = UseFieldTree(pinia);
const fieldTree = getFieldTree();
// 父组件数据
const props = defineProps<{ modelValue: T }>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
const message = useMessage();
const onPositiveClick = async () => {
  const id = modelValue.value.toDealRowId;
  const stageData = stageList.getStageDataById(id);
  if (stageData !== undefined) {
    // console.log(stageData.task,"++++++++++++++++++")
    if (TaskIdsModal.hasTask(stageData.task)) {
      message.error("存在任务，不可删除");
      return;
    }
    if (surfaceSetting.StageState === "finished") {
      message.error("已结束的阶段不可删除");
      return;
    }
    const fieldId = stageData.fatherId;
    const fieldData = fieldTree.getFieldDataById(fieldId);
    if (fieldData !== undefined) {
      StateMoal.deleteId(fieldData.stageIds, id, surfaceSetting.StageState);
    }
    const task = stageData.task;
    const list = new Set<number>();
    StateMoal.getAllId(task.TaskBased, list, true, true, true);
    StateMoal.getAllId(task.ClockBased, list, true, true, true);
    for (const number of list) {
      taskList.deleteTaskDataById(number);
    }
    stageList.deleteStageDataById(id);
    // if (surfaceSetting.StageState === "finished") {
    //   const finishStageList = await cacheFinishStageMap.getFinishStageList(stageData.fatherId);
    //   FinishStageList.deleteFinishStage(finishStageList, stageData.id);
    //   cacheFinishStageMap.changeKeys.push(finishStageList.id);
    // }
  }
  modelValue.value.toDelete = false;
  surfaceSetting.StageListKey = new Date().getTime();
  surfaceSetting.TaskListKey = new Date().getTime() + 1;
};
</script>
